package grafica;

import hotel.Autenticazione;
import hotel.Database;

import java.awt.EventQueue;

import javax.swing.*;

import java.awt.*;
import javax.swing.border.BevelBorder;

import server.AutenticaRegistrazione;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;
import java.sql.SQLException;

public class RecuperoPasswordGrafica {

	private JFrame frmRecuperoPassword;
	private JTextField usernameT;
	private JTextField emailT;
	private Database d;
	private SpringLayout springLayout;
	private JButton recPass;
	private JLabel caricamento;

	/**
	 * Create the application.
	 */
	public RecuperoPasswordGrafica(Database d) {
		this.d = d;
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frmRecuperoPassword = new JFrame();
		frmRecuperoPassword.setLocation(new Point(450, 100));
		String look = UIManager.getSystemLookAndFeelClassName();
		try {
			UIManager.setLookAndFeel(look);
		} catch (ClassNotFoundException | InstantiationException
				| IllegalAccessException | UnsupportedLookAndFeelException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		frmRecuperoPassword.getContentPane().setBackground(Color.LIGHT_GRAY);
		frmRecuperoPassword.setBackground(Color.LIGHT_GRAY);
		frmRecuperoPassword.setVisible(true);
		frmRecuperoPassword.setTitle("Recupero Password");
		frmRecuperoPassword.setSize(new Dimension(350, 250));
		frmRecuperoPassword.setResizable(false);
		frmRecuperoPassword
				.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		frmRecuperoPassword.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				new AccessoGrafica(d);
				frmRecuperoPassword.dispose();

			}
		});
		springLayout = new SpringLayout();
		frmRecuperoPassword.getContentPane().setLayout(springLayout);

		JLabel username = new JLabel("Username");
		springLayout.putConstraint(SpringLayout.SOUTH, username, -193,
				SpringLayout.SOUTH, frmRecuperoPassword.getContentPane());
		springLayout.putConstraint(SpringLayout.EAST, username, -140,
				SpringLayout.EAST, frmRecuperoPassword.getContentPane());
		username.setHorizontalAlignment(SwingConstants.CENTER);
		username.setAlignmentX(Component.CENTER_ALIGNMENT);
		username.setFont(new Font("Times New Roman", Font.PLAIN, 16));
		frmRecuperoPassword.getContentPane().add(username);

		JLabel email = new JLabel("Email");
		springLayout.putConstraint(SpringLayout.NORTH, email, 60,
				SpringLayout.NORTH, frmRecuperoPassword.getContentPane());
		springLayout.putConstraint(SpringLayout.EAST, email, -151,
				SpringLayout.EAST, frmRecuperoPassword.getContentPane());
		email.setFont(new Font("Times New Roman", Font.PLAIN, 16));
		frmRecuperoPassword.getContentPane().add(email);

		usernameT = new JTextField();
		springLayout.putConstraint(SpringLayout.NORTH, usernameT, 35,
				SpringLayout.NORTH, frmRecuperoPassword.getContentPane());
		springLayout.putConstraint(SpringLayout.WEST, usernameT, 108,
				SpringLayout.WEST, frmRecuperoPassword.getContentPane());
		usernameT.setFont(new Font("Times New Roman", Font.PLAIN, 14));
		usernameT.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null,
				null, null));
		frmRecuperoPassword.getContentPane().add(usernameT);
		usernameT.setColumns(10);

		emailT = new JTextField();
		springLayout.putConstraint(SpringLayout.WEST, emailT, 108,
				SpringLayout.WEST, frmRecuperoPassword.getContentPane());
		springLayout.putConstraint(SpringLayout.EAST, emailT, -101,
				SpringLayout.EAST, frmRecuperoPassword.getContentPane());
		springLayout.putConstraint(SpringLayout.EAST, usernameT, 0,
				SpringLayout.EAST, emailT);
		springLayout.putConstraint(SpringLayout.NORTH, emailT, 6,
				SpringLayout.SOUTH, email);
		emailT.setFont(new Font("Times New Roman", Font.PLAIN, 14));
		emailT.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null,
				null));
		frmRecuperoPassword.getContentPane().add(emailT);
		emailT.setColumns(10);

		recPass = new JButton("Recupera Password");
		springLayout.putConstraint(SpringLayout.WEST, recPass, 0,
				SpringLayout.WEST, usernameT);
		springLayout.putConstraint(SpringLayout.SOUTH, recPass, -28,
				SpringLayout.SOUTH, frmRecuperoPassword.getContentPane());
		recPass.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				if (usernameT.getText().equals("")) {
					JOptionPane
							.showMessageDialog(null, "Inserisci l'Username!");
				} else if (emailT.getText().equals("")) {
					JOptionPane.showMessageDialog(null, "Inserisci l'Email!");
				} else {
					attivaCaricamento();
					new Load(usernameT.getText(), emailT.getText(),
							frmRecuperoPassword, d).start();
				}
			}
		});
		recPass.setFont(new Font("Times New Roman", Font.BOLD, 12));
		recPass.setBackground(Color.LIGHT_GRAY);
		frmRecuperoPassword.getContentPane().add(recPass);

		ImageIcon imageIcon = new ImageIcon("Risorse/indicator.gif");
		caricamento = new JLabel("Connessione al server...", imageIcon,
				JLabel.CENTER);
		springLayout.putConstraint(SpringLayout.WEST, caricamento, 0, SpringLayout.WEST, usernameT);
		springLayout.putConstraint(SpringLayout.SOUTH, caricamento, -25, SpringLayout.NORTH, recPass);
		frmRecuperoPassword.getContentPane().add(caricamento);
		caricamento.setVisible(false);
	}

	public void attivaCaricamento() {
		caricamento.setVisible(true);
		recPass.setVisible(false);
	}

	public void disattivaCaricamento() {
		caricamento.setVisible(false);
		recPass.setVisible(true);
	}

	class Load extends Thread {
		private String pass, email;
		private JFrame r;
		private Database d;

		public Load(String pass, String email, JFrame r, Database d) {
			this.pass = pass;
			this.email = email;
			this.r = r;
			this.d = d;
		}

		public void run() {
			try {
				if (d.recuperaPassword(pass, email)) {
					new AccessoGrafica(d);
					r.dispose();
				}
				disattivaCaricamento();
			} catch (Exception e) {
				e.printStackTrace();
				disattivaCaricamento();
			}
		}
	}

}
